#ifndef ENEMY_H
#define ENEMY_H

#include "Framework.h"
#include "KdTree.h"
#include "particleEmitter.h"

class Enemy {
public:
	Enemy(GLdouble initX, GLdouble initY, GLdouble initZ, GLdouble initSpeed, KdTree *kdTree_) {	
		x = initX;
		y = initY;
		z = initZ;
		lx = 1.0;
		ly = 0.0;
		lz = 0.0;
		angleX = 0.0;
		angleY = 0.0;
		rotateX = 0.0;
		rotateY = 0.0;
		rx = 0.0;
		ry = 0.0;
		rz = 1.0;
		ux = 0.0;
		uy = 1.0;
		uz = 0.0;
		speed = initSpeed;
		kdTree = kdTree_;
		following = false;
                emitter.setPosition(aiVector3D(x, y, z));
                emitter.setParticleMode(3); //spark
		anim = 0;
	}


	void updatePosition() {
		aiVector3D oldEnemyPos(x, y, z);
		move(speed);
  		aiVector3D newEnemyPos(x, y, z);
		aiVector3D dir = newEnemyPos - oldEnemyPos;
		         
		float delta = 0.1;               
                if (kdTree->cameraCollisionTest(oldEnemyPos, dir)) {
                	aiVector3D dn = dir.Normalize();
		  	x = oldEnemyPos.x - (delta*dn).x;
		  	z = oldEnemyPos.z - (delta*dn).z;
		}
               
	}
	void strafe(GLdouble d) {
		x = x + rx * d;					
		//y = y + ry * d;
		z = z + rz * d;	
	}

	void move(GLdouble d) {
		x = x + lx * d;
		//y = y + ly * d;
		z = z + lz * d;	

                emitter.setPosition(aiVector3D(x, y, z));
	}

	void rotateXDegrees(GLdouble r) {
		//if (rotateX + r > -89 && rotateX + r < 89) {
			angleX = 0;
			angleX += r;
			rotateX += r;
			updateAfterRotateX();
 		//} else {
		//	printf("rotateY=%f\n", rotateX);		
		//}
	}

	void rotateYDegrees(GLdouble r) {
		//if (rotateY + r > -89 && rotateY + r < 89) {
			angleY = 0;
			angleY += r;
			rotateY += r;
			updateAfterRotateY();
		//} else {
		//	printf("rotateY=%f\n", rotateY);		
		//}
	}

	void toggleAnim() {
		if (anim == 0) {
			anim = 1;		
		} else {
			anim = 0;		
		}			
	}

        particleEmitter* getEmitter(){return &emitter;}

	GLdouble x;
	GLdouble lx;
	GLdouble y;
	GLdouble ly;
	GLdouble z;
	GLdouble lz;
	GLdouble angleX;
	GLdouble angleY;
	GLdouble rotateX;
	GLdouble rotateY;
	GLdouble rx;
	GLdouble ry;
	GLdouble rz;
	GLdouble ux;
	GLdouble uy;
	GLdouble uz;
	GLdouble speed;
	bool following;
	int anim;
private:
	KdTree *kdTree;
	int sceneId;

        particleEmitter emitter;

	void updateAfterRotateX() {
		rx = rx * cos(angleX * M_PI/180.0) + lx * sin(angleX * M_PI/180.0);
		//ry = ry * cos(angleX * M_PI/180.0) + ly * sin(angleX * M_PI/180.0);
		rz = rz * cos(angleX * M_PI/180.0) + lz * sin(angleX * M_PI/180.0);

		GLdouble l = sqrt(rx * rx + ry * ry + rz * rz);
		rx = rx / l;
		//ry = ry / l;
		rz = rz / l;
				
		lx = (ry * uz - rz * uy) * -1;
		//ly = (rz * ux - rx * uz) * -1;
		lz = (rx * uy - ry * ux) * -1;	
	}

	void updateAfterRotateY() {
		lx = lx * cos(angleY * M_PI/180.0) + ux * sin(angleY * M_PI/180.0);
		ly = ly * cos(angleY * M_PI/180.0) + uy * sin(angleY * M_PI/180.0);
		lz = lz * cos(angleY * M_PI/180.0) + uz * sin(angleY * M_PI/180.0);

		GLdouble l = sqrt(lx * lx + ly * ly + lz * lz);
		lx = lx / l;
		ly = ly / l;
		lz = lz / l;

		ux = (ly * rz - lz * ry) * -1;
		uy = (lz * rx - lx * rz) * -1;
		uz = (lx * ry - ly * rx) * -1;
	}

};

#endif
